package com.summer.plugin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component("redisPlugin")
public class RedisPlugin {

	@Autowired
	private StringRedisTemplate redisTemp;

	public void add(String key, String value) {
		ValueOperations<String, String> ops = this.redisTemp.opsForValue();
		ops.set(key, value);
	}

	public void add(String key, String value, int seconds, TimeUnit unit) {
		ValueOperations<String, String> ops = this.redisTemp.opsForValue();
		ops.set(key, value, seconds, unit);
	}
	public String get(String key) {
		return this.redisTemp.opsForValue().get(key);
	}

	public void delete(String key) {
		this.redisTemp.delete(key);
	}

	public void addListItem(String listName, String value) {
		BoundListOperations<String, String> list = redisTemp.boundListOps(listName);
		list.leftPush(value);
	}

	public void deleteListItem(String listName, String value) {
		BoundListOperations<String, String> list = redisTemp.boundListOps(listName);
		RedisOperations<String, String> ops = list.getOperations();
		ops.delete(value);
	}

	public void deleteListItem(String listName, List<String> values) {
		BoundListOperations<String, String> list = redisTemp.boundListOps(listName);
		RedisOperations<String, String> ops = list.getOperations();
		ops.delete(values);
	}

	public boolean existListItem(String listName, String value) {
		BoundListOperations<String, String> list = redisTemp.boundListOps(listName);
		RedisOperations<String, String> ops = list.getOperations();
		Long count = ops.getExpire(value);
		if (count == null) {
			return false;
		} else if (count <= 0) {
			return false;
		} else {
			return true;
		}
	}

	public List<String> getList(String listName) {
		BoundListOperations<String, String> list = redisTemp.boundListOps(listName);
		return list.range(0, list.size() - 1);
	}

	public void addMapItem(String mapName, String key, Object value) {
		BoundHashOperations<String, Object, Object> map = redisTemp.boundHashOps(mapName);
		map.put(key, value);
	}

	public void deleteMapItem(String mapName, String key) {
		BoundHashOperations<String, Object, Object> map = redisTemp.boundHashOps(mapName);
		map.getOperations().delete(key);
	}

	public String getMapItem(String mapName, String key) {
		BoundHashOperations<String, Object, Object> map = redisTemp.boundHashOps(mapName);
		Object value = map.get(key);
		if(value == null){
			return null;
		}else{
			return value.toString();
		}
	}
	
	public Map<Object, Object> getMap(String mapName) {
		BoundHashOperations<String, Object, Object> map = redisTemp.boundHashOps(mapName);
		return map.entries();
	}
	
	public void addSetItem(String setName, String value) {
		BoundSetOperations<String, String> list = redisTemp.boundSetOps(setName);
		list.add(value);
	}

	public void deleteSetItem(String setName, String value) {
		BoundSetOperations<String, String> list = redisTemp.boundSetOps(setName);
		RedisOperations<String, String> ops = list.getOperations();
		ops.delete(value);
	}

	public void deleteSetItem(String setName, List<String> values) {
		BoundSetOperations<String, String> list = redisTemp.boundSetOps(setName);
		RedisOperations<String, String> ops = list.getOperations();
		ops.delete(values);
	}

	public boolean existSetItem(String setName, String value) {
		BoundSetOperations<String, String> list = redisTemp.boundSetOps(setName);
		RedisOperations<String, String> ops = list.getOperations();
		Long count = ops.getExpire(value);
		if (count == null) {
			return false;
		} else if (count <= 0) {
			return false;
		} else {
			return true;
		}
	}
}
